package org.example.code.test2025.two_month;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class test0215 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int a = in.nextInt();
        long[] nums = new long[a];
        for (int i = 0; i < a; i++) {
            nums[i] = in.nextInt();
        }
        System.out.println(fun(a, nums));
    }

    public static long fun(int n, long[] nums) {
        long op = 0;
        for(int i=1;i<n;i++){
            if(nums[i]<nums[i-1]){
                op = op+nums[i-1]-nums[i];
                nums[i]=nums[i-1];
            }
        }
        return op;
    }
}
